<template>
  <el-dialog
    v-model="dialogVisible"
    :title="title"
    width="30%"
    :before-close="handleClose"
  >
    <div class="dictionary-dialog">
      <el-form
        inline
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="规格名称" prop="name" style="width: 100%">
          <el-input
            placeholder="输入规格名称"
            style="width: 100%"
            v-model="ruleForm.name"
            :disabled="ruleForm.type == '系统预设' ? true : false"
          />
        </el-form-item>

        <!-- <el-form-item label="规格类型" prop="type" style="width: 100%">
          <el-radio-group v-model="ruleForm.type">
            <el-radio label="系统预设">系统预设</el-radio>
            <el-radio label="用户自建">用户自建</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <el-form-item label="状态" prop="status" style="width: 100%">
          <el-radio-group
            v-model="ruleForm.status"
            :disabled="ruleForm.id ? true : false"
          >
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="规格属性" prop="spec" style="width: 100%">
          <div
            style="margin-bottom: 5px; width: 100%"
            v-for="(item, i) in ruleForm.specArr"
          >
            <el-input v-model="item.name" style="width: 50%" placeholder="属性">
              <template #suffix>
                <IEpCircleClose style="cursor: pointer" @click="delFun(i)" />
              </template>
            </el-input>
          </div>
          <div style="width: 100%">
            <el-button style="width: 50%" type="primary" plain @click="addSpec"
              >新增</el-button
            >
          </div>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="submit"> 提交 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { addApi, saveApi } from "@/plugins/api/shopping/specs";

let ruleForm = ref({
  status: 1,
  specArr: [],
});
let rules = {
  name: [{ required: true, message: "请输入规格名称", trigger: "blur" }],
  type: [{ required: true, message: "请选择规格类型", trigger: "blur" }],
  status: [{ required: true, message: "请选择状态", trigger: "blur" }],
};
let dialogVisible = ref(false);
function handleClose(done) {
  // dialogVisible.value = false;
  ElMessageBox.confirm("是否确定关闭此对话框?")
    .then(() => {
      done();
    })
    .catch(() => {
      // catch error
    });
}
let title = ref("");
let ruleFormRef = ref(null);
function initFun(type, row) {
  if (ruleFormRef.value) {
    ruleFormRef.value.resetFields();
  }
  ruleForm.value = {};
  if (type == "add") {
    title.value = "新增";
    // ruleForm.value.type = "系统预设";
    ruleForm.value.status = 1;
    ruleForm.value.specArr = [];
  } else {
    title.value = "编辑";
    ruleForm.value = Object.assign({}, row);
    if (row.spec) {
      ruleForm.value.specArr = row.spec.split(",").map((item) => {
        return {
          name: item,
        };
      });
    }
  }

  dialogVisible.value = true;
}
function addSpec() {
  ruleForm.value.specArr.push({ name: "" });
}
const emit = defineEmits(["refreshList"]);
function submit() {
  if (!ruleFormRef.value) return;
  ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      if (ruleForm.value.specArr.length > 0) {
        if (
          ruleForm.value.specArr.some((item) => {
            return item.name == "";
          })
        ) {
          return ElMessage.warning("请填写规格属性！");
        }
      } else {
        return ElMessage.warning("请填写规格属性！");
      }
      let obj = Object.assign({}, ruleForm.value);
      obj.spec = obj.specArr
        .map((ele) => {
          return ele.name;
        })
        .join(",");

      if (!ruleForm.value.id) {
        addApi(obj).then((data) => {
          ElMessage.success("新增成功！");
          dialogVisible.value = false;
          emit("refreshList");
        });
      } else {
        saveApi(obj).then((data) => {
          ElMessage.success("修改成功！");
          dialogVisible.value = false;
          emit("refreshList");
        });
      }
    }
  });
}
function delFun(i) {
  ruleForm.value.specArr.splice(i, 1);
}
onMounted(() => {});
defineExpose({
  initFun,
});
</script>
<style scoped lang="scss"></style>
